package com.zwh.autoconfigure.web;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

/**
 * @author monkey
 * @date 2020/6/12 15:53
 **/
@Configuration
public class HttpMessageConvertersConfig{

    /**
     * 定义fastjson消息转换器
     * @param
     */
    @Bean
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        //自定义配置...
        FastJsonConfig config = new FastJsonConfig();
        config.setCharset(StandardCharsets.UTF_8);

        //值为null时也要输出
        config.setSerializerFeatures(SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.DisableCircularReferenceDetect);

        /**
         * 支持请求格式
         */
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON);
        fastMediaTypes.add(new MediaType("application", "*+json"));
        converter.setSupportedMediaTypes(fastMediaTypes);

        converter.setFastJsonConfig(config);
        return converter;
    }

    /* 必须要设置全部消息转换器，否则只设置一个StringHttpMessageConverter，其余默认的消息转换器不会被默认设置进来
    WebMvcConfigurationSupport.getMessageConverters()方法中进行消息转换器的初始化
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add( this.stringHttpMessageConverter());
    }*/


//    /**
//     * 请求uri后缀返回对应的相应数据格式处理
//     * 默认为相应所有请求
//     */
//    @Override
//    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
//        //configurer.favorPathExtension(false);
//        configurer.mediaType(MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_XML);
//        configurer.mediaType(MediaType.APPLICATION_JSON_VALUE,MediaType.APPLICATION_JSON)
//    }
}
